//
//  FZHomeStatusPictureView.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/25/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit


private let pictureItemId = "PictureItemId"
private let itemMargin: CGFloat = 5
private let itemW: CGFloat = 80

class FZHomeStatusPictureView: UICollectionView {

//MARK:==========================属性==========================
    
    var pictureInfo: [FZHomePictureInfo]? {
        didSet{
            
            if let pic = pictureInfo where pic.count > 0{
                
                let size = getPictureSize(pic.count)
                
                self.snp_updateConstraints { (make) in
                    
                    make.size.equalTo(size)
                    make.bottom.equalTo(-PADDING)
                }
                
            }else{
                self.snp_updateConstraints { (make) in
                    make.size.equalTo(CGSizeZero)
                    make.bottom.equalTo(0)
                }
            }
            reloadData()
        }
    }
    
    private func getPictureSize(count: Int) -> CGSize{
        
        // 计算出行数和列数
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
    
        // 计算size 中的宽和高
        let w = CGFloat(col) * itemW + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row) * itemW + CGFloat(row - 1) * itemMargin
        
        
        return CGSize(width: w, height: h)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
        self.snp_makeConstraints { (make) in
            make.size.equalTo(CGSizeZero)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//MARK:==========================加载界面==========================
    func setupUI(){
        
        self.backgroundColor = TheTableColor
        let flowlayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        flowlayout.itemSize = CGSizeMake(itemW, itemW)

        
        flowlayout.minimumInteritemSpacing = itemMargin
        flowlayout.minimumLineSpacing = itemMargin
        
        //注册cell
        self.registerClass(FZHomeStatusPictureCell.self, forCellWithReuseIdentifier: pictureItemId)
        self.dataSource = self
        self.delegate = self
    }
}

extension FZHomeStatusPictureView: UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfo?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureItemId, forIndexPath: indexPath) as! FZHomeStatusPictureCell
        cell.urlStirng = pictureInfo![indexPath.item].thumbnail_pic

        return cell
    }
    
    //MARK:==========================跳转==========================
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//         let picturInfo = pictureInfo![indexPath.item]
        
    }
}


class FZHomeStatusPictureCell: UICollectionViewCell {
    
    var urlStirng: String?{
        didSet{
//            bgIMGView.fz_setImage(urlStirng!, placeholderImgName: "weibo_placeholder")
            bgIMGView.fz_downloadIMG(urlStirng!, placeholder: "weibo_placeholder")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //MARK:==========================加载界面==========================
    
    func setupUI() {
        contentView.addSubview(bgIMGView)
        contentView.clipsToBounds = true
    
    }
    func setupLayout(){
        bgIMGView.snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
    }
 
//MARK:==========================懒加载==========================
    lazy var bgIMGView: UIImageView =
        {
            let im: UIImageView = UIImageView()
            im.contentMode = .ScaleAspectFill
            return im
    }()
}
